﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RuoVea.ExEnum;
using RuoVea.ExFilter;
using RuoVea.ExSugar;
using RuoVea.OmiApi.Auth.Entitys;
using SqlSugar;

namespace RuoVea.OOrgMenuRoleUser.Template.Controllers;

/// <summary>
/// 
/// </summary>
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    private readonly ILogger<WeatherForecastController> _logger;
    private readonly SugarRepository<SysUser> _sysUser;
    private readonly  ISqlSugarClient  _sqlSugarClient;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="logger"></param>
    public WeatherForecastController(ILogger<WeatherForecastController> logger, SugarRepository<SysUser> sysUser,ISqlSugarClient sqlSugarClient)
    {
        _logger = logger;
        _sysUser=sysUser;
        _sqlSugarClient = sqlSugarClient;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }

    /// <summary>
    /// Swagger登录检查 🔖
    /// </summary>
    /// <returns></returns>
    [AllowAnonymous]
    [NonRestfulResult]
    [HttpPost("/api/swagger/checkUrl")]
    public int SwaggerCheckUrl()
    {
        return 200;// _httpContextAccessor.HttpContext.User.Identity.IsAuthenticated ? 200 : 401;
    }

    /// <summary>
    /// Swagger登录提交 🔖
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    /// <returns></returns>
    [AllowAnonymous]
    [NonRestfulResult]
    [HttpPost("/api/swagger/submitUrl")]
    public int SwaggerSubmitUrl([FromForm] string userName, [FromForm] string password)
    {
        try
        {
            return 200;
        }
        catch (Exception)
        {
            return 401;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [AllowAnonymous]
    [NonRestfulResult]
    [HttpGet("/api/SubUrl")]
    public bool SubUrl()
    {
        Gender sex = ((new Random()).Next(1, 2) == 1 ? ExEnum.Gender.MALE : ExEnum.Gender.FEMALE);
        _sysUser.Update(new SysUser() { Sex = sex, Id = 142307070910553 });

        return _sqlSugarClient.Updateable<SysUser>(new SysUser() { Sex =  Gender.UNKNOWN, Id = 142307070910553 }).IgnoreNullColumns(true).EnableDiffLogEvent().ExecuteCommand() > 0;
    }
}
